import {mergecart,cartlist} from '@/Api/cart'
let cart = {
    namespaced: true,
    state: {
        list: []
    },
    mutations: {
        addList(state, obj) {
            state.list.push(obj)
        },
        changeNum(state, obj) {
            state.list[obj.index].count += obj.number
        },
        // 取反
        changeone(state, index) {
            state.list[index].selected = !state.list[index].selected
        },
        // 全选
        changeall(state, flag) {
            state.list.forEach(item => item.selected = flag)
        },
        // 减少数量
        jian(state,index) {
            if(state.list[index].count<=1){
                state.list[index].count = 1
            }else{
                state.list[index].count--
            }
           
        },
        // 添加数量
        jia(state, index) {
            state.list[index].count++
        },
        // 批量删除
        dell(state) {
            state.list = state.list.filter(item => !item.selected)
        },
        // 单个删除
        del(state, key) {
            console.log(111);
            state.list.splice(key, 1)
        },
        clearList(state){
            state.list=[]
        },
        setList(state,arr){
            state.list=arr
        }
    },
    getters: {
        changeList(state) {
            let sum = 0
            state.list.forEach(item => {
                sum += item.count
            })
            return sum
        },
        // 购物车页面选中的总数量
        numberFlag(state) {
            var sum = 0
            state.list.forEach(item => {
                if (item.selected) {
                    sum += item.count
                }
            });
            return sum
        },
        // 选中商品的总价格
        sumPrice(state) {
            var sum = 0
            state.list.forEach(item => {
                if (item.selected) {
                    sum += item.count * item.nowPrice
                    // console.log(sum);
                }

            })
            //   console.log(sum);
            return sum
        },
        // 商品的总价格
        sumPrice2(state) {
            var sum = 0
            state.list.forEach(item => {
                sum += item.count * item.nowPrice

            })
            //   console.log(sum);
            return sum
        },
        // 单项商品的价格
        sumPrice3(state) {
            var sum = 0
            state.list.forEach(item => {
                sum = item.count * item.nowPrice

            })
            //   console.log(sum);
            return sum
        },
        mergelist(state){
            // console.log(state.list);
            let arr=[]
             state.list.forEach(item=>{
                let obj={skuId:item.skuId,selected:item.selected,count:item.count}
               arr.push(obj)
            })
            console.log(arr);
            return arr
        }
    },
    actions:{
        async asyncmergecart (context){
            await mergecart(context.getters.mergelist)
            context.commit('clearList')
            
        },
        async asynccartlist (context){
            let {data:res}=await cartlist()
            context.commit ("setList",res.result)
            // console.log(res);
        }
    }
}

export default cart



